//动态类型
function fun<T>(a: T, b: T): Array<T> {
    return [a, b]
}

fun(1, 2)
fun('1', '2')

type ty<T> = number | string | T
let x: ty<null> = '1'

interface Data<T> {
    msg: T
}

let y: Data<string> = {
    msg: '1'
}

function add<T = number, K = string>(a: T, b: K): Array<T | K> {
    return [a, b]
}

add('1', 1)
add({}, null)

const axios = {
    get<T>(url: string): Promise<T> {
        return new Promise((resolve, reject) => {
            let xhr: XMLHttpRequest = new XMLHttpRequest()
            xhr.open('GET', url)
            xhr.onreadystatechange = () => {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
            xhr.send(null)
        })
    }
}

interface Data1 {
    message: string,
    code: number
}

axios.get<Data1>('./命令.md').then((res) => {
    console.log(res)
})


// 泛型约束
interface Len {
    length: number
}

function f<T extends Len>(a: T) {
    return a.length
}
f([12, 34])
f('1111')

// 约束对象key  keyof
let p = {
    name: 'wyx',
    age: 18
}
type Key = keyof typeof p      //所有属性名组成的联合类型  //"age" | "name"
function objKey<T extends object, K extends keyof T>(obj: T, key: K) {
    console.log(obj[key])
}
objKey(p, 'age')

// 高级用法（看不懂思密达）
interface Text {
    name?: string
    order: number
    flag: boolean
}
type Option<T extends Text> = {
    readonly [Key in keyof T]: T[Key]
}
type D = Option<Text>